use algorithm::sort::{bubble_sort, merge_sort, quick_sort};
use dialoguer::{Select, theme::ColorfulTheme};
fn main() {
    let theme = ColorfulTheme::default();
    let items = vec!["冒泡排序", "归并排序", "快速排序", "退出"];
    let arr = vec![10, 5, 46, 32, 98, 67, 26, 61, 58, 66, 20, 19];
    loop {
        let selection = Select::with_theme(&theme)
            .with_prompt("请按动上下箭头选择您希望执行的算法")
            .items(&items)
            .interact()
            .unwrap();
        println!(
            "您选择的索引是{}，选择的内容是{}",
            selection, items[selection]
        );
        let ret = match selection {
            0 => bubble_sort::bubble_sort(&arr),
            1 => merge_sort::merge_sort(&arr),
            2 => quick_sort::quick_sort(&arr),
            _ => {
                println!("您退出了程序");
                break;
            }
        };
        print_arr_sort_before(&arr);
        print_arr_sort_after(&ret);
    }
}

fn print_arr_sort_before(arr: &[i32]) {
    println!("原始数组为：");
    print_arr(arr);
}

fn print_arr_sort_after(arr: &[i32]) {
    println!("排序后数组为：");
    print_arr(arr);
}

fn print_arr(arr: &[i32]) {
    let _ = arr.iter().for_each(|element| {
        print!("{}\t", element);
    });
    println!();
}
